#region using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;

#endregion


namespace Controls
{
    /// <summary>
    /// Builds a list of child pages for the current page.
    /// </summary>
    public class ChildPageList :Control
    {
        /// <summary>
        /// Initializes the <see cref="ChildPageList"/> class.
        /// </summary>
        static ChildPageList()
        {
            BlogEngine.Core.Page.Saved += delegate { _Html = null; };
        }

        #region Properties

        private static object _SyncRoot = new object();
        private static string _Html;
        /// <summary>
        /// Caches the rendered HTML in the private field and first
        /// updates it when a post has been saved (new or updated).
        /// </summary>
        private string Html
        {
            get
            {
                if (_Html == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_Html == null && SiteMap.CurrentNode != null && SiteMap.CurrentNode.HasChildNodes)  // || BlogEngine.Core.Page.Items == null)
                        {
                            HtmlGenericControl ul = BuildChildPageList();

                            using (System.IO.StringWriter sw = new System.IO.StringWriter())
                            {
                                ul.RenderControl(new HtmlTextWriter(sw));
                                _Html = sw.ToString();
                            }
                        }
                    }
                }

                return _Html;
            }
        }

        #endregion

        /// <summary>
        /// Loops through all pages and builds the HTML
        /// presentation.
        /// </summary>

        /// <summary>
        /// Loops through all child pages and builds html UL list collection.
        /// </summary>
        private static HtmlGenericControl BuildChildPageList()
        {
            HtmlGenericControl ul = new HtmlGenericControl("ul");
            ul.Attributes.Add("class", "pagelist");
            ul.ID = "childpagelist";

            foreach (SiteMapNode node in SiteMap.CurrentNode.ChildNodes)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlAnchor anchor = new HtmlAnchor();

                anchor.HRef = node.Url;
                anchor.InnerHtml = node.Title;
                anchor.Title = node.Description;

                li.Controls.Add(anchor);
                ul.Controls.Add(li);
            }

            return ul;
        }

        /// <summary>
        /// Renders the control.
        /// </summary>
        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.Write(Html);
            writer.Write(Environment.NewLine);
        }
    }
}